// pages/order/orderGoing/orderGoing.js
const handler = require("../../../utils/function.js");
var app = getApp(),
    api = require("../../../api.js");
Page({
    /**
     * 页面的初始数据
     */
    data: {
        markers: [{
            iconPath: "../../../images/driver/icon-30.png",
            id: 0,
            longitude: 108.94,
            latitude: 34.28,
            width: 15,
            height: 27
        }],
        polyline: [{
            points: [{
                longitude: wx.getStorageSync("startAddress").longitude,
                latitude: wx.getStorageSync("startAddress").latitude
            }, {
                longitude: 108.94,
                latitude: wx.getStorageSync("startAddress").latitude
            }],
            color: "#0299de",
            width: 9,
            dottedLine: false,
            arrowLine: true,
        }],
        controls: [{
            id: 0,
          iconPath: '../../../images/user/icon-30.png',
            position: {
                left: 10,
                top: 10,
                width: 50,
                height: 50
            },
            clickable: true
        }],
        longitude: null,
        latitude: null,
        order_no: '',
        clears: 0
    },
  controltap(e) {
    console.log("map controltap", e.controlId)
    const addresses = handler.getLatLntHandler((res) => {
      //  console.log("一键定位",res);
      this.setData({
        longitude: res.startAddress.location.lat,
        latitude: res.startAddress.location.lng,
      })
    });
  },
  // 修改用户和一键定位位置
  changeIcon: function () {
    const that = this;
    const {
      controls
    } = this.data;
    wx.getSystemInfo({
      success: function success(res) {
        var controlsH = res.windowWidth / 750 * 130; //计算一像素的宽度 res.windowWidth
        var map_height = res.windowHeight - controlsH;
        controls[0] = {
          id: 0,
          iconPath: '../../../images/user/icon-30.png',
          position: {
            top: map_height - 56,
            left: 10,
            width: 50,
            height: 50
          },
          clickable: true
        };
        that.setData({
          winPath: {
            w: res.windowWidth,
            h: res.windowHeight
          },
          map_height,
          controls,
        });
      }
    });
  },
  // 获取位置路线
  getPolyline: function() {
      handler.getPolyline((res) => {
          const polyline = this.data.polyline;
          polyline[0].points = res;
          this.setData({
              polyline
          });
      })
  },

    // intoPageHandler 页面跳转
    intoPageHandler: function(e) {
        var that = this;
        console.log('结束行程结果', that.data.order_no);
        app.request({
            url: api.order.orderend,
            method: 'post',
            data: {
                order_no: e.currentTarget.dataset.order_no,
                longitude: e.currentTarget.dataset.longitude,
                latitude: e.currentTarget.dataset.latitude
            },
            success: function(rs) {
                if (rs.code == 1) {
                    handler.intoPageHandler(e.currentTarget.dataset.skip)
                } else {
                    wx.showToast({
                        title: rs.msg,
                    })
                   
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log('开始行程', options)
        var that = this;
        that.setData({
            order_no: options.order_no,
        })
        // that.getPolyline();
        // const time = setInterval(() => {
        //     console.log('状态', that.data.clears);
        //     if (that.data.clears == 2) {
        //         clearTimeout(time);
        //     } else {
        //         that.timeTask()
        //     }
        // }, 1000);
      this.changeIcon();
    },
    // 时间定时器
    timeTask: function() {
        var that = this;
        app.request({
            url: api.real_time.geting,
            method: 'post',
            data: {
                order_no: that.data.order_no,
            },
            success: function(rs) {
                console.log('经纬度', rs.data)
                // that.setData({
                //     longitude: rs.data.list[1],
                //     latitude: rs.data.list[0],
                //     clears: rs.data.order.is_service
                // })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
        console.log('开始行程结果', that.data.order_no);
        app.request({
            url: api.order.getfind,
            method: 'post',
            data: {
                order_no: that.data.order_no,
            },
            success: function(rs) {
                if (rs.code == 1) {
                    that.setData({
                        order: rs.data,
                        markers: rs.data.slocaltion,
                        driver: wx.getStorageSync("driver"),
                        longitude: rs.data.elocaltion.lng,
                        latitude: rs.data.elocaltion.lat,
                    })
                    wx.setStorageSync("startAddress", rs.data.slocation)
                    wx.setStorageSync("endAddress", rs.data.elocation)
                      that.getPolyline();
                } else {
                    wx.showToast({
                        title: rs.msg,
                    })
                    wx.redirectTo({
                      url: '',
                    })
                    wx.redirectTo({
                      url: '../orderedStatus/orderedStatus?order_no=' + that.data.order_no,
                    })
                }
            }
        })
    },
})